import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 牛客网: DP31 买卖股票的最好时机(二)
 * <a href="https://www.nowcoder.com/practice/fbc5dad3e215457fb82a3ae688eb7281?tpId=230&tqId=2364576&ru=%2Fpractice%2F351b87e53d0d44928f4de9b6217d36bb&qru=%2Fta%2Fdynamic-programming%2Fquestion-ranking&sourceUrl=">...</a>
 * User: DELL
 * Date: 2023-08-23
 * Time: 23:05
 */
public class Main {
    /**
     * 解题思路:
     * 因为该题不限制买入卖出股票的次数,且买入卖出均无手续费,
     * 则直接可以将当前数组看作是折线图,那么这个股票的最大收益
     * 其实就是所有上涨区间相加.直接遍历原数组,将上涨区间加起
     * 来即可.
     * @param args
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        int profit = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i] > arr[i-1]) {
                profit += arr[i] - arr[i-1];
            }
        }
        System.out.println(profit);
    }
}
